package cn.hls.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import cn.hls.dao.SmsAdviceDao;
import cn.hls.entity.SmsAdvice;
import cn.hls.entity.vo.PageInfo;
import cn.hls.until.GenericsUtils;

 
/**
  *@fileName: SmsAdviceDaoImpl.java
  *@package: cn.hls.dao.impl
　*@Description:短信通知数据操作接口实现类
　*@author: yinghui.zhang
　*@email: uuhui@163.com
  *@date: 2016年11月13日
 *
 */
@Component("smsAdviceDao")
public class SmsAdviceDaoImpl extends BaseDaoImpl<SmsAdvice> implements
		SmsAdviceDao {

	@Override
	public boolean exists(SmsAdvice entity) throws Exception {
		List<Object>params=new ArrayList<Object>();
		StringBuilder hql=new StringBuilder("select count(id) from  SmsAdvice p where p.deleted=? ");
		params.add(false);
		hql.append("and p.type=?  and p.sourceId=? ");
		params.add(entity.getType());
		params.add(entity.getSourceId());
		if(StringUtils.hasText(entity.getSourceCode())){
			hql.append("and p.sourceCode=? ");
			params.add(entity.getSourceCode().trim());
		}else{
			hql.append("and p.sourceCode is null ");
		}
		hql.append("and p.createTime>=? ");
		
		params.add(GenericsUtils.getPreDateTime(new Date(), entity.getDateDiff()));
		Long c= countByHql(hql.toString(),params.toArray());
		return c.intValue()>0?true:false;
	}

	@Override
	public PageInfo<SmsAdvice> loadSmsAdviceInfo(PageInfo<SmsAdvice> page,
			SmsAdvice entity) throws Exception {
		StringBuilder selectHql=new StringBuilder("from SmsAdvice p where p.deleted=? ");
		List<Object>params=new ArrayList<Object>();
		params.add(false);
		if(entity!=null){
			//按短信通知类型查询
			if(entity.getType()!=null){
				selectHql.append("and p.type=? ");
				params.add(entity.getType());
			}
			//按短信通知对象编号查询
			if(entity.getSourceId()!=null){
				selectHql.append("and p.sourceId=? ");
				params.add(entity.getSourceId());
			}
			//按短信通知名称查询
			if(StringUtils.hasText(entity.getSendName())){
				selectHql.append("and p.sendName like ? ");
				params.add("%"+entity.getSendName().trim()+"%");
			}
			//按短信通知手机号查询
			if(StringUtils.hasText(entity.getSendPhone())){
				selectHql.append("and p.sendPhone like ? ");
				params.add("%"+entity.getSendPhone().trim()+"%");
			}
		}
		StringBuilder countHql=new StringBuilder("select count(id) ").append(selectHql);
		selectHql.append("order by createTime desc ");
		return find(selectHql.toString(), countHql.toString(), params.toArray(), page);
	}

}
